#include "StdAfx.h"
#include "resource.h"
#include "NodeX.h"

CNode::CNode()
{
    m_type = 0;
    m_east = m_north = m_south = m_west = NULL;

    m_prev = NULL;
    m_dfs = INT_MAX;
}

CNode::CNode(int id)
{
    CNode();
    m_id = id;
}

CNode::~CNode()
{
}

void CNode::Paint(CPaintDC& dc)
{
    // Create DCs
    CDC* mdc = new CDC;
    mdc->CreateCompatibleDC(&dc);

    CBitmap bmp;
    int id;

    if (m_type & NF_SELECTED)
    {
        if ((m_type & NF_INOUT) == NF_INOUT)
            id = IDB_INOUT_SEL;
        else if ((m_type & NF_INLET) == NF_INLET)
            id = IDB_INLET_SEL;
        else if ((m_type & NF_OUTLET) == NF_OUTLET)
            id = IDB_OUTLET_SEL;
        else
            id = IDB_NODE_SEL;
    }
    else        
    {
        if ((m_type & NF_INOUT) == NF_INOUT)
            id = IDB_INOUT;
        else if ((m_type & NF_INLET) == NF_INLET)
            id = IDB_INLET;
        else if ((m_type & NF_OUTLET) == NF_OUTLET)
            id = IDB_OUTLET;
        else
            id = IDB_NODE;
    }

    bmp.LoadBitmap(id);
    mdc->SelectObject(&bmp);

    dc.BitBlt(m_pt.x - 4, m_pt.y - 4, 8, 8, mdc, 0, 0, SRCCOPY);

    delete mdc;

    // Draw line to all interconnects
    DrawLineTo(m_north, dc, RGB(0, 0, 0));
    DrawLineTo(m_south, dc, RGB(0, 0, 0));
    DrawLineTo(m_east, dc, RGB(0, 0, 0));
    DrawLineTo(m_west, dc, RGB(0, 0, 0));
}

void CNode::DrawLineTo(CNode* n, CPaintDC& dc, COLORREF cr)
{
    if (n != NULL)
    {
        int offsetX, offsetY;

        // Get x-offset
        if (m_pt.x > n->m_pt.x)
            offsetX = -4;
        else if (m_pt.x < n->m_pt.x)
            offsetX = 4;
        else
            offsetX = 0;

        // Get y-offset
        if (m_pt.y > n->m_pt.y)
            offsetY = -4;
        else if (m_pt.y < n->m_pt.y)
            offsetY = 4;
        else
            offsetY = 0;

        // Check for 2-way street (thicker line)
        int thickness;
        if (n->m_east == this || n->m_north == this || n->m_south == this || n->m_west == this)
            thickness = 2;
        else
            thickness = 1;

        CPen pen(PS_SOLID, thickness, cr);
        CPen* pOldPen = dc.SelectObject(&pen);
        dc.MoveTo(m_pt.x + offsetX, m_pt.y + offsetY);
        dc.LineTo(n->m_pt.x - offsetX, n->m_pt.y - offsetY);
        dc.StrokePath();
        dc.SelectObject(pOldPen);
    }
}